package com.example.CarManagement.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.CarManagement.common.Result;
import com.example.CarManagement.enumPojo.OrderStatus;
import com.example.CarManagement.sys.dto.OrderDto;
import com.example.CarManagement.sys.entity.Orders;
import com.example.CarManagement.sys.service.impl.OrdersServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 订单表，记录派车信息和实际用车情况 前端控制器
 * </p>
 *
 * @author 徐佳彪
 * @since 2025-07-23
 */
@RestController
@RequestMapping("/sys/orders")
public class OrdersController {

    @Resource
    private OrdersServiceImpl ordersService;

    // 查询订单
    @PreAuthorize("@se.hasPermi('ORDER_SELECT')")
    @GetMapping("/orderList")
    public Result<?> orderList(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam(required = false) String applicationId, @RequestParam(required = false) String orderNumber) {
        return ordersService.getPageList(pageNo, pageSize, applicationId, orderNumber);
    }

    // 司机获取任务
    @PreAuthorize("@se.hasPermi('ORDER_SELECT')")
    @GetMapping("/dispatchTask")
    public Result<?> dispatchTask(@RequestHeader("Authorization") String token, @RequestParam("status") OrderStatus orderStatus) {
        return ordersService.dispatchTask(token, orderStatus);
    }

    @PreAuthorize("@se.hasPermi('ORDER_SELECT')")
    @GetMapping("/getById/{id}")
    public Result<?> getById(@PathVariable String id, @RequestHeader("Authorization") String token) {
        return ordersService.getInfoById(id, token);
    }

    @PreAuthorize("@se.hasPermi('ORDER_SELECT')")
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable String id) {
        return Result.success(ordersService.getById(id));
    }


    @PreAuthorize("@se.hasPermi('ORDER_COMPLETE')")
    @PutMapping("/completeTask")
    public Result<?> completeTask(@RequestBody OrderDto orderDto) {
        return ordersService.completeTask(orderDto);
    }


    // 司机开始任务
    @PreAuthorize("@se.hasPermi('ORDER_STARTTASK')")
    @PutMapping("/startTask/{id}")
    public Result<?> startTask(@PathVariable String id) {
        return ordersService.startTask(id);
    }

    // 修改订单（燃油费用 其他费用，实际行驶里程(公里)，  状态）
    @PreAuthorize("@se.hasPermi('ORDER_UPDATE')")
    @PutMapping("/update")
    public Result<?> updateOrder(@RequestBody Orders orders) {
        return Result.success(ordersService.updateById(orders));
    }


    // 评价订单
    @PreAuthorize("@se.hasPermi('ORDER_UPDATE')")
    @PutMapping("/rate")
    public Result<?> commentOrder(@RequestBody Orders orders) {

        Orders byId = ordersService.getById(orders);

        if (byId == null) {
            return Result.fail("订单不存在");
        }

        if (byId.getStatus() != OrderStatus.completed) {
            return Result.fail("订单状态错误");
        }

        if (byId.getRating() != null) {
            return Result.fail("订单已评价");
        }

        byId.setRating(orders.getRating());
        byId.setFeedback(orders.getFeedback());
        ordersService.updateById(byId);

        return Result.success("评价成功");
    }

}
